//
//  BaseButton.m
//  DuBai
//
//  Created by darktm on 14-2-24.
//  Copyright (c) 2014年 darktm. All rights reserved.
//

#import "BaseTimeButton.h"

@implementation BaseTimeButton

@synthesize seconds = _seconds;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

-(NSTimeInterval)seconds{

    
    return _seconds <= 0 ? 0 : _seconds;
}

-(void)setSeconds:(NSTimeInterval)seconds{

    _seconds = seconds;
    self.timer =  [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(on_timer_change) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:UITrackingRunLoopMode];
}

-(void)on_timer_change{

    _seconds--;
    
    if(_seconds <= 0){
        
        self.enabled = NO;
        [self.timer invalidate];
        self.timer = nil;
    }
    [self updateLableValue];
}

-(void)updateLableValue{
    [self setTitle:[NSString stringWithFormat:@"%.0f",_seconds] forState:UIControlStateNormal];
}

-(void)dealloc{

    if([self.timer isValid]){
        [self.timer invalidate];
        self.timer = nil;
    }
}

@end
